package co.fitstart.weapp.api.dao;

import java.util.Date;
import java.util.List;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;

import co.fitstart.entity.course.CourseType;
import co.fitstart.entity.course.ResultReport;
import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.course.round.RoundCoursewareStatus;
import co.fitstart.entity.course.round.RoundStatus;
import co.fitstart.entity.order.Order;
import co.fitstart.entity.principal.Principal;
import co.fitstart.entity.user.Student;
import co.fitstart.entity.user.User;

/**
 * 
 * @author dan
 *
 */
@Dao("hibernate")
public interface CourseRoundDao {

    /**
     * 
     * @param student
     * @param courseType
     * @param courseStatuss
     * @return
     */
    @Query("FROM CourseRound WHERE disabled = false AND student = :student AND courseType = :courseType AND courseStatus IN (:courseStatuss)")
    public List<CourseRound> list(@Variable("student") Student student, @Variable("courseType") CourseType courseType, @Variable("courseStatuss") List<RoundStatus> courseStatuss);

    /**
     * 
     * @param student
     * @param courseTypes
     * @param courseStatus
     * @return
     */
    @Query("FROM CourseRound WHERE disabled = false AND student = :student AND courseType in (:courseTypes) AND courseStatus = :courseStatus")
    public List<CourseRound> list(@Variable("student") Student student, @Variable("courseTypes") List<CourseType> courseTypes, @Variable("courseStatus") RoundStatus courseStatus);

    /**
     * 
     * @param student
     * @param courseTypes
     * @param courseStatuss
     * @return
     */
    @Query("FROM CourseRound WHERE disabled = false AND student = :student AND courseType in (:courseTypes) AND courseStatus IN (:courseStatuss) ORDER BY courseStatus ASC,createTime DESC")
    public List<CourseRound> list(@Variable("student") Student student, @Variable("courseTypes") List<CourseType> courseTypes, @Variable("courseStatuss") List<RoundStatus> courseStatuss);
    
    /**
     * 
     * @param orders
     * @param courseStatus
     * @return
     */
    @Query("FROM CourseRound WHERE order IN (:orders) AND courseStatus = :courseStatus ORDER BY createTime DESC")
    public List<CourseRound> listFinishCourseRound(@Variable("orders") List<Order> orders, @Variable("courseStatus") RoundStatus courseStatus);

    /**
     * 
     * @param orders
     * @param courseStatus
     * @return
     */
    @Query("FROM CourseRound WHERE order IN (:orders) AND courseStatus not in (:courseStatus) ORDER BY createTime DESC")
    public List<CourseRound> listUnfinishCourseRound(@Variable("orders") List<Order> orders, @Variable("courseStatus") List<RoundStatus> courseStatus);
    
    /**
     * 
     * @param courseRound
     * @return
     */
    @Query("SELECT COUNT(*) FROM StudentTargetData WHERE disabled = false AND courseRound = :courseRound")
    public int countStudentTargetData(@Variable("courseRound") CourseRound courseRound);

    /**
     * 
     * @param student
     * @return
     */
    @Query("SELECT COUNT(*) FROM CourseRound WHERE disabled = false AND student = :student AND courseStatus != :courseStatus")
    public int count(@Variable("student") Student student, @Variable("courseStatus") RoundStatus courseStatus);

    /**
     * 
     * @param courseRound
     * @return
     */
    @Query("FROM ResultReport WHERE disabled = false AND courseRound = :courseRound")
    public ResultReport loadResultReport(@Variable("courseRound") CourseRound courseRound);

    /**
     * 
     * @param user
     * @param courseStatuss
     * @return
     */
    @Query("SELECT MAX(effectiveEnd) FROM CourseRound WHERE disabled = false AND student.user = :user AND courseStatus IN (:courseStatuss) AND effectiveEnd IS NOT NULL")
    public Date getMaxEffectiveDate(@Variable("user") User user, @Variable("courseStatuss") List<RoundStatus> courseStatuss);

    /**
     * 
     * @param coach
     * @param roundCoursewareStatus
     * @param roundStatus
     * @return
     */
    @Query("FROM CourseRound WHERE disabled = false AND coach = :coach AND roundCoursewareStatus = :roundCoursewareStatus AND courseStatus IN (:roundStatuss) AND course.experience = false AND courseType IN (:courseTypes)")
    public List<CourseRound> list(@Variable("coach") Principal coach, @Variable("roundCoursewareStatus") RoundCoursewareStatus roundCoursewareStatus, @Variable("roundStatuss") List<RoundStatus> roundStatuss, @Variable("courseTypes") List<CourseType> courseTypes);

    /**
     * 
     * @param coach
     * @param roundCoursewareStatus
     * @param roundStatus
     * @return
     */
    @Query("FROM CourseRound WHERE disabled = false AND coach = :coach AND (roundCoursewareStatus = :roundCoursewareStatus OR courseStatus = :roundStatus) AND course.experience = false AND courseType IN (:courseTypes)")
    public List<CourseRound> list(@Variable("coach") Principal coach, @Variable("roundCoursewareStatus") RoundCoursewareStatus roundCoursewareStatus, @Variable("roundStatus") RoundStatus roundStatus, @Variable("courseTypes") List<CourseType> courseTypes);

    /**
     * 
     * @param coach
     * @param roundStatus
     * @return
     */
    @Query("FROM CourseRound WHERE disabled = false AND coach = :coach AND courseStatus = :roundStatus AND course.experience = false AND courseType IN (:courseTypes)")
    public List<CourseRound> list(@Variable("coach") Principal coach, @Variable("roundStatus") RoundStatus roundStatus, @Variable("courseTypes") List<CourseType> courseTypes);

    /**
     * 
     * @param coach
     * @param roundStatuss
     * @param courseTypes
     * @return
     */
    @Query("FROM CourseRound WHERE disabled = false AND coach = :coach AND courseStatus IN (:roundStatuss) AND course.experience = false AND courseType IN (:courseTypes)")
    public List<CourseRound> list(@Variable("coach") Principal coach, @Variable("roundStatuss") List<RoundStatus> roundStatuss, @Variable("courseTypes") List<CourseType> courseTypes);

    /**
     * 
     * @param student
     * @param courseTypes
     * @return
     */
    @Query("FROM CourseRound WHERE disabled = false AND student = :student AND courseType IN (:courseTypes) ORDER BY createTime DESC")
    public List<CourseRound> list(@Variable("student") Student student, @Variable("courseTypes") List<CourseType> courseTypes);

    /**
     * 
     * @param student
     * @param courseTypes
     * @param courseStatuss
     * @return
     */
    @Query("SELECT COUNT(*) FROM CourseRound WHERE disabled = false AND student = :student AND courseType IN (:courseTypes) AND course.experience = false AND course.give = true AND courseStatus IN (:courseStatuss)")
    public int countUnfinishedGive(@Variable("student") Student student, @Variable("courseTypes") List<CourseType> courseTypes, @Variable("courseStatuss") List<RoundStatus> courseStatuss);
}
